package com.appkubes.mall.admin.application.article.service;

import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.admin.application.article.dao.ArticleDao;
import com.appkubes.mall.core.po.MallArticleExample;
import com.appkubes.mall.core.po.MallArticleWithBLOBs;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ArticleService {

    @Autowired
    ArticleDao articleDao;

    public PageInfo querySelective(Integer page, Integer size, String sort, String order,String appKey) {
        MallArticleExample example = new MallArticleExample();
        MallArticleExample.Criteria criteria = example.createCriteria();
        criteria.andAppKeyEqualTo(appKey);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, size);
        List<MallArticleWithBLOBs> list = articleDao.selectByExampleWithBLOBs(example);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public MallArticleWithBLOBs findById(String articleKey) {
        return articleDao.selectByPrimaryKey(articleKey);
    }

    public int add(MallArticleWithBLOBs model) {
        model.setArticleKey(UUIDUtils.getUUID());
        model.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        model.setAppKey(null);
        return articleDao.insertSelective(model);
    }

    public int update(MallArticleWithBLOBs model) {
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        return articleDao.updateByPrimaryKeySelective(model);
    }

    public int deleteById(String articleKey) {
        return articleDao.deleteByPrimaryKey(articleKey);
    }

    public int deleteByIds(String[] ids) {
        return articleDao.delete(ids);
    }

    public int batchenable(String[] ids) {
        return articleDao.batchenable(ids);
    }

    public int batchunenable(String[] ids) {
        return articleDao.batchunenable(ids);
    }


}
